package com.zby.scms.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zby.scms.entity.Role;
import com.zby.scms.service.RoleService;
import com.zby.scms.shiro.ShiroConfig;
import com.zby.scms.utils.BaseResult;
import com.zby.scms.utils.ResultUtil;
import org.apache.catalina.LifecycleState;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

/**
 * role(Role)表控制层
 *
 * @author zhangboyi
 * @since 2019-12-31 19:02:44
 */
@Api(tags = "role(Role)") 
@RestController
@RequestMapping("role")
public class RoleController {
    /**
     * 服务对象
     */
    @Autowired
    private RoleService roleService;

    private Logger logger = LoggerFactory.getLogger(RoleController.class);
    /**
     * 通过主键查询单条数据
     *
     * @param roleid 主键
     * @return 单条数据
     */
    @ApiOperation(value = "根据id查询 role")
    @GetMapping("queryRoleById")
    public BaseResult<Role> queryRoleById(Integer roleid) {
        return ResultUtil.success(roleService.queryById(roleid));
    }

    @ApiOperation(value = "查询所有角色/*（下拉框去掉status为0的,前端传参）*/")
    @GetMapping("queryAllRole")
    public BaseResult<List<Role>> queryAllRole(Role role,Integer pageNum, Integer pageSize){
        if (pageNum == null||pageSize == null){
            logger.info("请求的参数不能为空");
            return ResultUtil.error(400, "请求的参数不能为空");
        }
        PageHelper.startPage(pageNum, pageSize);
        List<Role> roleList = roleService.queryAllRole(role);
        PageInfo<Role> pageInfo = new PageInfo<>(roleList);


    /*    List<Role> rolelist2 = null;

        //去掉status为0的角色
        for(Role roles : roleList){
            if(roles.getStatus()!=0){
                rolelist2.add(roles);
            }
        }*/
        return ResultUtil.success(
                pageInfo.getList(),
                pageInfo.getPageNum(),
                pageInfo.getPageSize(),
                pageInfo.getTotal());
    }

    @ApiOperation(value = "编辑角色信息id不可以修改")
    @PutMapping("updateRoleById")
    public BaseResult<Role> updateRole(Role role){
        logger.info("执行updateRole方法");
        //更新之前先判断数据库是否存在相同角色,排除自身

/*        if(queryRolename(role.getRolename()).equals("角色已存在")){
            return ResultUtil.error(400, "角色名已存在");

        }*/
        //
        Role r = roleService.queryRoleByName(role.getRolename());
        if(r!=null&&r.getRoleid()!=role.getRoleid()){
            return ResultUtil.error(400, "角色名已存在");
        }



        Role roles = roleService.update(role);
        logger.info(roles.toString());
        return ResultUtil.success(roles);



    }

    @ApiOperation(value = "查看角色是否存在")
    @GetMapping("queryRolename")
    public String queryRolename(String rolename){
        Role role = new Role();
        role.setRolename(rolename);
        //查询roleid是否存在
        if(roleService.queryAllRole(role).size()==0){

            return "角色不存在";

        }

        logger.info("角色已存在");
        return "角色已存在";
    }

    @ApiOperation("添加角色")
    @PostMapping("addRole")
    public BaseResult<Role> addRole(Role role){

        if(role.getRolename()==null){
            return ResultUtil.error(400, "角色名称不能为空");
        }

        if(queryRolename(role.getRolename()).equals("角色已存在")){
            return ResultUtil.error(400, "角色名已存在");
        }
        if(role.getStatus()==null){
            role.setStatus(1);
        }
        return ResultUtil.success(roleService.addRole(role));
    }

    @ApiOperation("删除角色")
    @DeleteMapping("deleteRole")
    public BaseResult<Role> deleteRole(Integer roleid){
        Role role = roleService.queryById(roleid);
        if(role==null){
            return ResultUtil.error(400,"角色ID不存在");
        }
        if(!roleService.deleteById(roleid)){
            return ResultUtil.error(500,"数据库操作有误");
        }

        logger.info(role.toString());
        return ResultUtil.success("角色删除成功",role);



    }

}